<?php

namespace App\Http\Controllers\Admin;

use App\Models\House;
use App\Models\Organize;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HouseController extends Controller
{
    public function index(Request $request)
    {
        $limit = $request->get('limit', 10);
        $city_id = $request->get('city_id', 0);
        $keys = $request->get('keys');
        $model = House::query();
        if ($keys) {
            $model = $model->where('title', 'like', '%' . $keys . '%');
        }
        if ($city_id) {
            $model = $model->where('city_id', $city_id);
        }
        $result = $model->orderBy('sort', 'asc')->orderBy('id', 'desc')->paginate($limit);

        return showMsg('操作成功', 200, $result);
    }

    public function getHouse()
    {
        $model = House::query();
        $model = $model->select('id', 'title', 'city_id', 'province_id');
        $result = $model->orderBy('id', 'desc')->get();
        return showMsg('操作成功', 200, $result);
    }

    public function detail(Request $request)
    {
        $id = $request->get('id');
        $result = House::findOrFail($id);
        if (!$result) {
            return showMsg('信息已删除', 201, $result);
        }
        if ($result['province_id']) {
            $class[0] = $result['province_id'];
            $class[1] = $result['city_id'];
            $result['class_id'] = $class;
        }
        if ($result['drawing']) {
            $result['drawing'] = getAliOssImage($result['drawing']);
        }
        if ($result['project']) {
            $result['project'] = getAliOssImage($result['project']);
        }
        if ($result['thumb']) {
            $result['thumb'] = getAliOssImage($result['thumb']);
        }
        if ($result['video']) {
            $result['video'] = getAliOssImage($result['video']);
        }
        if ($result['tags']) {
            $result['tags'] = explode(',', $result['tags']);
        }
        if ($result['imgs']) {
            $imgs = explode(',', $result['imgs']);
            foreach ($imgs as $key=> $val){
                $imgs[$key] = getAliOssImage($val);
            }
            $result['imgs'] = $imgs;
        } else {
            $result['imgs'] = [];
        }
        if ($result['house']) {
            $house = explode(',', $result['house']);
            foreach ($house as $key=> $val){
                $house[$key] = getAliOssImage($val);
            }
            $result['house'] = $house;
        } else {
            $result['house'] = [];
        }
        if ($result['photos1']) {
            $photos1 = explode(',', $result['photos1']);
            foreach ($photos1 as $key=> $val){
                $photos1[$key] = getAliOssImage($val);
            }
            $result['photos1'] = $photos1;
        } else {
            $result['photos1'] = [];
        }
        if ($result['photos2']) {
            $photos2 = explode(',', $result['photos2']);
            foreach ($photos2 as $key=> $val){
                $photos2[$key] = getAliOssImage($val);
            }
            $result['photos2'] = $photos2;
        } else {
            $result['photos2'] = [];
        }
        if ($result['photos3']) {
            $photos3 = explode(',', $result['photos3']);
            foreach ($photos3 as $key=> $val){
                $photos3[$key] = getAliOssImage($val);
            }
            $result['photos3'] = $photos3;
        } else {
            $result['photos3'] = [];
        }
        if ($result['content']) {
            $result['content'] = replaceOssImgSrc($result['content']);
        }
        return showMsg('操作成功', 200, $result);
    }

    public function beforeSave(Request $request)
    {
        $id = $request->get('id');
        $class_id = $request->get('class_id');
        $house = $request->get('house');
        $imgs = $request->get('imgs');
        $tags = $request->get('tags');
        $photos1 = $request->get('photos1');
        $photos2 = $request->get('photos2');
        $photos3 = $request->get('photos3');
        $data = $request->except(['class_id', 'house', 'imgs', 'tags', 'photos1', 'photos2', 'photos3']);
//        if ($class_id) {
//            $data['province_id'] = $class_id[0];
//            $data['city_id'] = $class_id[1];
//        }
        $data['house'] =delJsonAliOssImage($house);
        $data['imgs'] = delJsonAliOssImage($imgs);
        $data['photos1'] = delJsonAliOssImage($photos1);
        $data['photos2'] = delJsonAliOssImage($photos2);
        $data['photos3'] = delJsonAliOssImage($photos3);
        $data['tags'] = implode(',', $tags);

        if($data['drawing']){
            $data['drawing'] = delAliOssImage($data['drawing']);
        }
        if($data['project']){
            $data['project'] = delAliOssImage($data['project']);
        }
        if($data['thumb']){
            $data['thumb'] = delAliOssImage($data['thumb']);
        }
        if ($data['video']) {
            $data['video'] = delAliOssImage($data['video']);
        }
        if ($data['content']) {
            $data['content'] = replaceImgSrc($data['content']);
        }
        if ($id) {
            $article = House::findOrFail($id);
            if ($article->update($data)) {
                return showMsg('更新成功', 200);
            }
        } else {
            House::create($data);
            return showMsg('添加成功', 200);
        }
    }

    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return showMsg('请选择操作项', 201);
        }
        if (is_array($ids)) {
            foreach ($ids as $v) {
                House::destroy('id', $v);
            }
        } else {
            House::destroy('id', $ids);
        }
        return showMsg('删除成功', 200);
    }

    public function setIndex(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');
        House::where('id', $id)->update(['is_index' => $status]);
        return showMsg('设置成功', 200);
    }

    public function setPuts(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');
        House::where('id', $id)->update(['is_put' => $status]);
        return showMsg('设置成功', 200);
    }
}
